package com.it.nongchanping.controller;

import com.it.nongchanping.entity.Announcement;
import com.it.nongchanping.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

/**
 * 公告控制器
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 公告列表页
     */
    @GetMapping("/list")
    public String announcementList(Model model) {
        List<Announcement> announcementList = announcementService.getAllAnnouncements();
        model.addAttribute("announcementList", announcementList);
        return "announcement/list";
    }

    /**
     * 公告详情页
     */
    @GetMapping("/detail/{id}")
    public String announcementDetail(@PathVariable Long id, Model model) {
        Announcement announcement = announcementService.getAnnouncementById(id);
        if (announcement == null) {
            return "redirect:/announcement/list";
        }
        model.addAttribute("announcement", announcement);
        return "announcement/detail";
    }
} 